"""
Case Type   : CM
Case Name   : 使用gs_guc set -Z cmagent -N all -I all -c方式修改参数alarm_report_max_count值为2
Create At   : 2024.6
Owner       : haomeng802
Description :
    1、查看集群状态 cm_ctl query -Cidvp
    2、查询参数alarm_report_max_count默认值
    3、修改其值为2，并reload使其生效
    4、查询修改后的参数alarm_report_max_count的值
    5、恢复参数默认值
    6、重启集群
Expect      :
    1、集群状态正常，数据库节点状态正常
    2、默认值查询成功为1
    3、修改参数值为2成功，reload成功
    4、查询修改后的参数值为2
    5、恢复参数默认值成功
    6、重启集群成功
History     :
    2024.4.11 ningyali 增加前置（安装cm）和后置（卸载cm）操作
"""

import os
import unittest

from testcase.utils.ComInstall import CmInstall
from testcase.utils.ComInstall import CmUnInstall
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class CmServer(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.param = 'alarm_report_max_count'
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.root_nodes = (Node('PrimaryRoot'), Node('Standby1Root'),
                           Node('Standby2Root'))
        self.user_nodes = (Node('PrimaryDbUser'), Node('Standby1DbUser'),
                           Node('Standby2DbUser'))
        self.common = Common()

        text = '----prestep1:安装cm;expect:执行成功----'
        self.log.info(text)
        self.install_pkg_path = os.path.dirname(macro.DB_SCRIPT_PATH)
        self.cm = CmInstall(self.install_pkg_path, self.pri_sh.node.ssh_user)
        res = self.cm.update_xml_for_cm(*self.root_nodes)
        self.assertGreater(res.count('cmDir'), 1, f'执行失败: {text}')

        res = self.cm.cm_install(self.user_nodes[0])
        self.assertIn('Install CM tool success', res, f'执行失败: {text}')

    def test_cm_server(self):
        text = '-----step1:查看集群状态; expect:集群状态正常-----'
        self.log.info(text)
        status = self.pri_sh.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)

        text = '----step2:查询参数alarm_report_max_count默认值; expect:默认值查询成功为1----'
        self.log.info(text)
        cmd = f'source {macro.DB_ENV_PATH};' \
              f'cm_ctl list --param --agent | grep alarm_report_max_count'
        self.log.info(cmd)
        show_param1 = self.user_nodes[0].sh(cmd).result()
        self.log.info(show_param1)
        self.assertEqual('1',
                         show_param1.splitlines()[0].split('=')[-1].strip(),
                         '参数默认值不为1')

        text = '-----step3:修改其值为2，并reload使其生效 expect:修改参数值为2成功，reload成功-----'
        self.log.info(text)
        set_cmd = f'source {macro.DB_ENV_PATH};' \
                  f'gs_guc set -Z cmagent -N all -I all -c "{self.param} = 2";' \
                  f'cm_ctl reload --param --agent'
        self.log.info(set_cmd)
        set_res = self.user_nodes[0].sh(set_cmd).result()
        self.log.info(set_res)
        self.assertIn('ALL: Success to perform gs_guc', set_res, '参数设置失败')
        self.assertIn(self.constant.cma_reload_success_msg, set_res, '参数加载失败')

        text = '-----step4:查询修改后的参数值  expect:参数值为2-----'
        self.log.info(text)
        show_param2 = self.user_nodes[0].sh(cmd).result()
        self.log.info(show_param2)
        self.assertEqual('2',
                         show_param2.splitlines()[0].split('=')[-1].strip(),
                         '参数值未修改成功')

        text = '-----step5:恢复参数默认值  expect:恢复默认值成功-----'
        self.log.info(text)
        set_cmd = f'source {macro.DB_ENV_PATH};' \
                  f'gs_guc set -Z cmagent -N all -I all -c "{self.param} = 1";' \
                  f'cm_ctl reload --param --agent'
        self.log.info(set_cmd)
        set_res = self.user_nodes[0].sh(set_cmd).result()
        self.log.info(set_res)
        self.assertIn('ALL: Success to perform gs_guc', set_res, '参数设置失败')
        self.assertIn(self.constant.cma_reload_success_msg, set_res, '参数加载失败')

    def tearDown(self):
        text = '----poststep1:卸载cm;expect:执行成功----'
        self.log.info(text)
        self.un_cm = CmUnInstall(self.install_pkg_path,
                                 self.pri_sh.node.ssh_user)
        res = self.un_cm.cm_uninstall(
            self.user_nodes[0], '--deleteData --deleteBinary')
        self.assertIn('Uninstall CM tool success', res, f'执行失败: {text}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
